package vinicorp.dolphin.movingviewthread;
//Hello SVN 2
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.movingviewthread.R;

public class MainActivity extends Activity {

	private int mTime = 0;
	private DemNguocRunnable mDemnguocRun;
	private Handler mDemnguocHandler;
	ImageView animatedView3;
	LinearLayout mainLayout;
	int imgWidth, imgHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
		animatedView3 = (ImageView) findViewById(R.id.imageView1);
		BitmapDrawable bd = (BitmapDrawable) this.getResources().getDrawable(
				R.drawable.logo);
		imgWidth = bd.getBitmap().getWidth();
		imgHeight = bd.getBitmap().getHeight();
		mTime = (-1) * (imgWidth);

		animatedView3.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				animatedView3.clearAnimation();
				mDemnguocHandler.removeCallbacks(mDemnguocRun);

				mDemnguocHandler.postDelayed(mDemnguocRun, 1000);
				RotateAnimation anim = new RotateAnimation(0, 360,
						animatedView3.getX() + imgWidth / 2, imgHeight / 2);

				anim.setDuration(1000);
				animatedView3.setAnimation(anim);
				return false;
			}
		});

		mDemnguocHandler = new Handler();
		mDemnguocRun = new DemNguocRunnable();
		showDemNguoc();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void showDemNguoc() {
		animatedView3.setX(mTime);
		mDemnguocHandler.removeCallbacks(mDemnguocRun);
		mDemnguocHandler.postDelayed(mDemnguocRun, 1000);
	}

	private class DemNguocRunnable implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			handleDemnguoc();
		}

	}

	private void handleDemnguoc() {
		if (mTime < mainLayout.getWidth()) {
			mTime++;

		} else
			mTime = (-1) * (imgWidth - 3);

		animatedView3.setX(mTime);
		mDemnguocHandler.postDelayed(mDemnguocRun, 20);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mDemnguocHandler.removeCallbacks(mDemnguocRun);
	}

}
